package com.github.tvbox.osc.ui.dialog;

import android.view.View;
import android.widget.TextView;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.UiThread;
import androidx.collection.ArraySet;

/**
 * 整点报时
 */
public class Clock {

    private SimpleDateFormat format;
    private Callback callback;
    private Date date;
    private String formatString;
    private Set<TextView> views;
    private Timer timer;

    public static Clock create() {
        return new Clock();
    }

    public static Clock create(TextView view) {
        return new Clock().view(view).format("yyyy/MM/dd HH:mm");
    }

    public Clock() {
    }

    public Clock view(TextView view) {
        if (views == null) {
            this.views = new ArraySet<>();
        }
        this.views.add(view);
        return this;
    }

    public Clock view(Set<TextView> views) {
        this.views = views;
        return this;
    }

    public Clock format(String format) {
        this.formatString = format;
        return this;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void start() {
        //开始一个定时器 设计每秒更新
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                doJob();
                View view = View();
                if (view != null) {
                    view.post(updateViewTask);
                }
            }
        }, 0, 1000);
    }

    private void doJob() {
        try {
            if (date == null) {
                date = new Date();
            }
            if (format == null) {
                this.format = new SimpleDateFormat(formatString, Locale.getDefault());
            }
            date.setTime(System.currentTimeMillis());
            if (callback != null) callback.onTimeChanged();
            //UI
            formatTime = format.format(date);

        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }

    private String formatTime;

    // 更新时间View的任务
    public Runnable updateViewTask = this::updateView;

    // UI
    @UiThread
    private void updateView() {

        for (TextView view : views) {
            //检查一下是不是需要变更
            if (view != null) {
                if (!formatTime.contentEquals(view.getText())) {
                    view.setText(formatTime);
                }
            }
        }
    }

    private View View() {
        for (View v : views) {
            if (v != null) {
                return v;
            }
        }
        return null;
    }

    public Clock stop() {
        if (timer != null) timer.cancel();
        return this;
    }

    public void release() {
        if (timer != null) timer.cancel();
        if (callback != null) callback = null;
    }

    public interface Callback {

        void onTimeChanged();
    }
}
